package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.AlarmPushDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.SignalMachineDto;
import com.hualude.roadsign.model.HighLeverEntity;
import com.hualude.roadsign.model.MachineFaultEntity;
import com.hualude.roadsign.model.SignalMachineEntity;
import com.hualude.roadsign.repository.AlarmPushRepository;
import com.hualude.roadsign.repository.AlarmRepository;
import com.hualude.roadsign.repository.SignalMachineFaultRepository;
import com.hualude.roadsign.repository.SignalMachineRepository;
import com.hualude.roadsign.service.SignalMachineManageService;
import com.hualude.roadsign.utils.FormatNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by smarteacup on 2017/7/25.
 */
@Service
public class SignalMachineManageServiceImpl implements SignalMachineManageService {

    @Resource
    private SignalMachineRepository signalMachineRepository;

    @Resource
    private SignalMachineFaultRepository signalMachineFaultRepository;

    @Resource
    private AlarmRepository alarmRepository;

    @Resource
    private AlarmPushRepository alarmPushRepository;

    @Override
    public DTResponseDto getSignalMachine4DT(DTRequestDto dtRequestDto,
                                             String actorIds,
                                             Integer orgId,
                                             Boolean audit,
                                             String startRoad,
                                             String endRoad,
                                             Integer alarmBinding) throws Exception {
        List<SignalMachineDto> signalMachineDtos = signalMachineRepository.getSignalMachine4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, alarmBinding);
        Integer count = signalMachineRepository.getSignalMachine4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, alarmBinding);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, signalMachineDtos);
        return dtResponseDto;
    }

    @Override
    public List<SignalMachineDto> getSignalMachine4APP(
            String actorIds, Integer orgId, Integer mCount,
            Integer mSize, String city, String startRoad, String endRoad, String alarmType) throws Exception {
        return signalMachineRepository.getSignalMachine4APP(
                actorIds, orgId, mCount, mSize, city, startRoad, endRoad, alarmType);
    }

    @Override
    public SignalMachineDto getSignalMachineById(Integer signalMachineById) throws Exception {
        return signalMachineRepository.getSignalMachineById(signalMachineById).get(0);
    }

    @Override
    public List<String> getStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return signalMachineRepository.getStartRoad(roadName, actorId, orgId);
    }

    @Override
    public List<String> getSectionByStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return signalMachineRepository.getSectionByStartRoad(roadName, actorId, orgId);
    }

    @Override
    @Transactional
    public Integer deleteSignalMachine(String signalMachineIds) throws Exception {
        String[] signalMachineIdArray = signalMachineIds.split(",");
        for (String sinalMachineId : signalMachineIdArray) {
            signalMachineFaultRepository.deleteMachineFault(Integer.parseInt(sinalMachineId));
            signalMachineRepository.deleteSignalMachine(Integer.parseInt(sinalMachineId));
        }
        return signalMachineIdArray.length;
    }

    @Override
    @Transactional
    public Integer addSignalMachine(SignalMachineDto signalMachineDto) throws Exception {
        signalMachineDto.setSignalMachineLongitude(FormatNumber.resize(signalMachineDto.getSignalMachineLongitude()));
        signalMachineDto.setSignalMachineLatitude(FormatNumber.resize(signalMachineDto.getSignalMachineLatitude()));
        Integer devicePathId = signalMachineRepository.save(signalMachineDto.toBean()).getSignalMachineId();
        String deviceId = signalMachineDto.getSignalMachineADeviceId();
        if (!deviceId.isEmpty()) {
            alarmRepository.updapteAlarmPath(1, devicePathId, deviceId);
        }
        return devicePathId;
    }

    @Override
    @Transactional
    public Integer updateSignalMachine(SignalMachineDto signalMachineDto) throws Exception {
        return signalMachineRepository.updateSignalMachine(
                signalMachineDto.getSignalMachineId(),
                signalMachineDto.getProjectNumber(),
                signalMachineDto.getSignalMachineRegionId(),
                signalMachineDto.getSignalMachineLongitude(),
                signalMachineDto.getSignalMachineLatitude(),
                signalMachineDto.getSignalMachineProvince(),
                signalMachineDto.getSignalMachineCity(),
                signalMachineDto.getSignalMachineArea(),
                signalMachineDto.getSignalMachineStartRoad(),
                signalMachineDto.getSignalMachineEndRoad(),
                signalMachineDto.getSignalMachineType(),
                signalMachineDto.getSignalMachineBrand(),
                signalMachineDto.getSignalMachineNumber(),
                signalMachineDto.getSignalMachineIsAlarm(),
                signalMachineDto.getSignalMachinePhoto(),
                signalMachineDto.getSignalMachineInstallTime(),
                signalMachineDto.getSignalMachineModifierId(),
                signalMachineDto.getSignalMachinePeriod(),
                signalMachineDto.getSignalMachineServiceLife(),
                signalMachineDto.getSignalMachineRemark(),
                signalMachineDto.getSignalMachineInstallOrg(),
                signalMachineDto.getSignalMachineMainOrgId(),
                0);
    }

    @Override
    public List<String> getCity4SignalMachine(String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return signalMachineRepository.getCity4SignalMachine(actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByCity(String city, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return signalMachineRepository.getStartRoadByCity(city, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<SignalMachineDto> getSignalMachine4Map4App(Integer orgId,
                                                           String actorIds,
                                                           double neLongitude,
                                                           double neLatitude,
                                                           double swLongitude,
                                                           double swLatitude) throws Exception {
        return signalMachineRepository.getSignalMachine4Map4App(orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
    }

    @Override
    @Transactional
    public Integer auditSignalMachine(String signalMachineIds) throws Exception {
        String[] signalMachineIdArray = signalMachineIds.split(",");
        for (String signalMachineId : signalMachineIdArray) {
            signalMachineRepository.auditSignalMachine(Integer.parseInt(signalMachineId));
        }
        return signalMachineIdArray.length;
    }

    @Override
    public List<String> getSectionByStartRoad4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return signalMachineRepository.getSectionByStartRoad4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Transactional
    @Override
    public Integer bindingDevice(String deviceId, Integer signalMachineId) throws Exception {
        SignalMachineEntity entity = signalMachineRepository.findOne(signalMachineId);
        StringBuffer position = new StringBuffer();
        position.append(entity.getSignalMachineProvince());
        position.append(entity.getSignalMachineCity());
        position.append(entity.getSignalMachineArea());
        position.append(" ");
        position.append(entity.getSignalMachineStartRoad());
        position.append("-");
        position.append(entity.getSignalMachineEndRoad());
        int count1 = signalMachineRepository.bindingDevice(deviceId, signalMachineId);
        count1 += alarmRepository.bindingDevice(3, signalMachineId, position.toString(), deviceId);
        return count1;
    }

    @Override
    @Transactional
    public Integer unbindingDevice(String deviceId, Integer highLeverId) throws Exception {
        int count = signalMachineRepository.unbindingDevice(highLeverId);
        count += alarmRepository.unbindingDevice(deviceId);
        return count;

    }

    @Override
    public DTResponseDto showAlarmHistory(DTRequestDto dtRequestDto, String deviceId, Integer deviceType) throws Exception {
        List<AlarmPushDto> dtos = alarmPushRepository.getAlarmHistory4DT(dtRequestDto, deviceId, deviceType);
        int count = alarmPushRepository.getAlarmHistoryCount4DT(dtRequestDto, deviceId, deviceType);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    public List<SignalMachineDto> getDeviceInfo(Integer deviceId) throws Exception {
        return signalMachineRepository.getDeviceInfo(deviceId);
    }

}
